<template>
  <view class="">
    <view v-for="(feeItem, feeIndex) in feeFormItem" :key="feeIndex">
      <!-- type 为 1 代表为费用 -->
      <stages-fee-item v-if="feeItem.type == 1" :feeList="feeFormItem" :feeIndex="feeIndex" />
      <!-- type 为 0 代表目录 -->
      <template v-else>
        <stages-fee-item
          :feeList="feeItem.childrenList"
          :feeIndex="feeChildIndex"
          v-for="(feeChildItem, feeChildIndex) in feeItem.childrenList"
          :key="feeChildIndex"
        />
      </template>
    </view>
  </view>
</template>
<script setup>
  import { inject, computed, watch } from 'vue'
  import stagesFeeItem from './stages-fee-item.vue'
  import { decimalAddition, decimalSubtract } from '@/utils'
  const props = defineProps({
    orderFeeIndex: Number,
  })
  const feeForm = inject('feeForm')
  const feeFormItem = computed(() => {
    return feeForm.value.orderFee[props.orderFeeIndex]?.feeDetailList
  })
  const feeSum = computed(() => {
    let sum = 0
    if (feeFormItem.value) {
      feeFormItem.value.forEach((feeItem) => {
        if (feeItem.type == 1) {
          sum = decimalAddition(sum, feeItem.amount)
        } else {
          let feeChildrenList = feeItem.childrenList
          if (feeChildrenList?.length > 0) {
            feeChildrenList?.forEach((feeChildItem) => {
              if (feeChildItem.costType == 20) {
                sum = decimalSubtract(sum, feeChildItem.amount)
              } else {
                sum = decimalAddition(sum, feeChildItem.amount)
              }
            })
          }
        }
      })
    }
    return sum
  })
  // 处理必填逻辑
  // const formAttrs = (item, index) => {
  //   if (item.itemName == '车辆租金' && item.feeCategory == 0) {
  //     return {
  //       required: true,
  //       prop: `orderFee.${props.orderFeeIndex}.feeDetailList.${index}.amount`,
  //     }
  //   } else {
  //     return {}
  //   }
  // }
  watch(
    () => feeSum.value,
    (newVal) => {
      if (feeForm.value.orderFee[props.orderFeeIndex]) {
        feeForm.value.orderFee[props.orderFeeIndex].activeTotalAmount = newVal
      }
    },
  )
</script>
<style lang="less" scoped></style>
